package com.ssm.gbq.controller;

import com.ssm.gbq.model.FlowerType;
import com.ssm.gbq.service.FlowerTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/flowerType")
public class FlowerTypeController {

    @Autowired
    private FlowerTypeService flowerTypeService;

    /**
     * 根据条件分页查询类型信息
     * @param flowerType
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showFlowerTypeByPage", method = RequestMethod.POST)
    public HashMap<String, Object> showFlowerTypeByPage(FlowerType flowerType, int currentPage, int pageSize){
        HashMap<String, Object> hashMap = new HashMap<>();
        //int offset = (currentPage-1)*pageSize;
        try {
            List<FlowerType> list = flowerTypeService.showFlowerTypeByPage(flowerType, currentPage, pageSize);
            hashMap.put("data", list);
            hashMap.put("count", flowerTypeService.getFlowerTypeCount(flowerType));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  hashMap;
    }

    /**
     * 根据ID查询类型信息
     * @param fid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/getFlowerTypeById")
    public Object getFlowerTypeById(Integer fid) throws Exception {
        FlowerType flowerType = flowerTypeService.getFlowerTypeById(fid);
        if(flowerType!=null){
            return flowerType;
        }else{
            return "未找到信息";
        }
    }

    /**
     * 修改
     * @param flowerType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/updateFlowerType")
    public HashMap<String, Object> updateFlowerType(FlowerType flowerType) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowerTypeService.updateFlowerType(flowerType);
        return map;
    }

    /**
     * 添加
     * @param flowerType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/addFlowerType")
    public HashMap<String, Object> addFlowerType(FlowerType flowerType) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowerTypeService.addFlowerType(flowerType);
        return map;
    }

    /**
     * 根据ID删除
     * @param fid
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/delFlowerType")
    public HashMap<String, Object> delFlowerType(Integer fid) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        int i = flowerTypeService.delFlowerType(fid);
        return map;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteFlowerTypes")
    public HashMap<String, Object> deleteFlowerTypes(Integer[] ids){
        HashMap<String, Object> map = new HashMap<>();
        flowerTypeService.deleteFlowerTypes(Arrays.asList(ids));
        return map;
    }

    /**
     * 判断所属类型是否为空
     * @param fid
     * @return
     */
    @ResponseBody
    @RequestMapping("/judgeType")
    public HashMap<String, Object> judgeType(Integer[] fid){
        HashMap<String, Object> map = new HashMap<>();
        int i = flowerTypeService.judgeType(Arrays.asList(fid));
        map.put("num", i);
        return map;
    }

}